package com.maybach.web.controller;


import com.maybach.core.ao.FlowApplyAO;
import com.maybach.core.form.flow.FlowActForm;
import com.maybach.core.form.flow.TaskActForm;
import com.maybach.web.constants.WebConstants;
import com.maybach.web.framework.util.WebUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by houenxun on 15/12/9.
 */
@Controller
@RequestMapping(value = "/flow/")
public class CommonAuditController {
    Logger logger = LoggerFactory.getLogger(this.getClass());


    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }

    /**
     * 通用审核操作
     *
     * @param request
     * @param redirectUrl
     * @param taskActForm
     * @param bindingResult
     * @param model
     * @param redirectAttributes
     * @return
     * @throws Exception
     */
    @Transactional
    @RequestMapping(value = "doAudit", method = RequestMethod.POST)
    @RequiresAuthentication
    public String doAudit(HttpServletRequest request, @RequestParam(required = false, value = "redirectUrl") String redirectUrl, @Valid @ModelAttribute("taskActForm") TaskActForm taskActForm, BindingResult bindingResult, ModelMap model, RedirectAttributes redirectAttributes) throws Exception {
        if (bindingResult.hasErrors()) {
            WebUtil.storeFlashAttribute(redirectAttributes, model);
            return "redirect:" + WebUtil.getLastUrlFromReferer(request);
        }

        model.addAttribute(WebConstants.REDIRECT_URL_KEY, redirectUrl);

        flowApplyAO.executeTask(taskActForm);
        return WebConstants.COMMON_RESULT_PAGE;

    }

    /**
     * 取消申请操作
     *
     * @param request
     * @param redirectUrl
     * @param flowActForm
     * @param bindingResult
     * @param model
     * @param redirectAttributes
     * @return
     * @throws Exception
     */
    @Transactional
    @RequiresAuthentication
    @RequestMapping(value = "doOperateFlow", method = RequestMethod.POST)
    public String doOperateFlow(HttpServletRequest request, String redirectUrl, @Valid @ModelAttribute() FlowActForm flowActForm, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) throws Exception {
        model.addAttribute(WebConstants.REDIRECT_URL_KEY, redirectUrl);
        if (bindingResult.hasErrors()) {
            WebUtil.storeFlashAttribute(redirectAttributes, model);
            return "redirect:" + WebUtil.getLastUrlFromReferer(request);
        }

        flowApplyAO.operaterFlow(flowActForm);
        return WebConstants.COMMON_RESULT_PAGE;

    }

    @Autowired
    private FlowApplyAO flowApplyAO;

}
